package com.yoda.mall.admin.service;

import com.yoda.mall.api.ProductCategoryApi;
import com.yoda.mall.model.dto.ProductCategoryDto;
import com.yoda.mall.model.entity.pms.ProductCategory;
import com.yoda.mall.model.entity.pms.ProductCategoryAttributeRelation;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 12月 18日 0:07
 */
@Service
public class ProductCategoryService {

    @DubboReference
    private ProductCategoryApi productCategoryApi;
    public List<ProductCategoryVo> GetCategoryWithChildren() {
        //读取所有分类信息
       List<ProductCategory> list = productCategoryApi.getAll();
       if (Objects.isNull(list)||list.size()==0){
           return new ArrayList<>();
       }
        List<ProductCategoryVo> vos = list.stream().map(p ->
                {
                    ProductCategoryVo vo = new ProductCategoryVo();
                    BeanUtils.copyProperties(p, vo);
                    return vo;
                }
        ).collect(Collectors.toList());
        //递归分类
        ProductCategoryVo father = new ProductCategoryVo();
        father.setId(0L);
        return recursive(father,vos);
    }

    private List<ProductCategoryVo> recursive(ProductCategoryVo father, List<ProductCategoryVo> children) {
        //根据子类数量,判断出口
        List<ProductCategoryVo> entities = children.stream().filter(p -> Objects.equals(p.getParentId(), father.getId())).collect(Collectors.toList());
        //如果没有子菜单,即返回
        if (entities.size()==0){
            return null;
        }
        List<ProductCategoryVo> list = children.stream().filter(p -> !Objects.equals(p.getParentId(), father.getId())).collect(Collectors.toList());
        //存在子类,遍历子类,寻找子子类
        return entities.stream().peek(entity -> {
            List<ProductCategoryVo> vos = recursive(entity, list);
            entity.setChildren(vos);
        }).collect(Collectors.toList());
    }

    public PageResult<ProductCategoryVo> getCategoryByPage(Long parentId, Integer page, Integer pageSize) {
        PageResult<ProductCategory> pageResult = productCategoryApi.getCategoryByPage(parentId,page,pageSize);
       if (Objects.isNull(pageResult)||pageResult.getTotal()==0){
           return new PageResult<>();
       }
        List<ProductCategoryVo> vos = pageResult.getList().stream().map(p ->
                {
                    ProductCategoryVo vo = new ProductCategoryVo();
                    BeanUtils.copyProperties(p, vo);
                    return vo;
                }
        ).collect(Collectors.toList());
        return new PageResult<>(page,pageSize,pageResult.getTotal(),vos);
    }

    public ProductCategory getCategoryById(Long id) {
        return productCategoryApi.getCategoryById(id);

    }

    public Integer createCategory(ProductCategoryDto dto) {
        //1.转换分类实体
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(dto,productCategory);
        List<Long> ids = dto.getProductAttributeIdList();
        return productCategoryApi.createCategory(productCategory,ids);
    }

    public Integer deleteCategoryById(Long id) {
        return productCategoryApi.deleteCategoryById(id);
    }

    public Integer updateCategoryById(Long id, ProductCategoryDto dto) {
        //转换实体
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(dto,productCategory);
        productCategory.setId(id);
        List<Long> ids = dto.getProductAttributeIdList();
        return productCategoryApi.updateCategoryById(productCategory,ids);
    }

    public Integer changeShowStatus(Integer showStatus, List<Long> ids) {
        return productCategoryApi.changeShowStatus(showStatus,ids);
    }

    public Integer changeNavStatus(Integer navStatus, List<Long> ids) {
        return productCategoryApi.changeNavStatus(navStatus,ids);
    }
}
